import json

from django.http import HttpResponse
from django.shortcuts import render

from employee.models import EmpDepartment
from functions.mssql_sql import mssql_function
from scanQrCode.models import *
from scanQrCode.scanClass.scanClass import EmpInfoFillIn, DropList
from scanQrCode.scanClass.scanClass import select_options

# Create your views here.
def sacnQr(request):
    return HttpResponse("提交成功")


'''同步信息'''


def suncData(request):
    '''部门表同步'''
    dept_list = mssql_function("select ID,DepartmentCode,DepartmentName,FullCode,FullName,IfUse,D_glgs,shortname,DeptLeve,ParentID from T_HR_Department",'select')
    for dept in dept_list:
        print(dept)
        EmpDepartment.objects.update_or_create(
            id=dept[0],
            defaults={
                'department_code': dept[1],
                'department_name': dept[2],
                'department_full_name': dept[4],
                'department_short_name': dept[7],
                'department_manage_attr': dept[6],
                'department_level': dept[8],
                'parent_id': dept[9] if dept[9] != 0 else 1
            }
        )
    '''民族表同步'''
    enthic_list = mssql_function("select ID,NationCode,NationName,IfUse from T_HR_Nation", 'select')
    for enthic in enthic_list:
        ScanEnthic.objects.update_or_create(
            id=enthic[0],
            defaults={
                'enthic_code': enthic[1],
                'enthic_name': enthic[2],
                'enthic_status': enthic[3]
            }
        )
    '''第一学历同步'''
    first_degree = mssql_function("select ID,DegreeCode,DegreeName,IfUse from T_HR_Degree", 'select')
    for degree in first_degree:
        ScanFirstDegree.objects.update_or_create(
            id=degree[0],
            defaults={
                'first_degree_code': degree[1],
                'first_degree_name': degree[2]
            }
        )
    '''籍贯同步'''
    native_list = mssql_function("select ID,HomeCode,HomeName,IfUse from T_HR_Home", 'select')
    for native in native_list:
        ScanNative.objects.update_or_create(
            id=native[0],
            defaults={
                'native_code': native[1],
                'native_name': native[2],
                'native_status': native[3]
            }
        )
    return HttpResponse("同步成功")


'''扫码填写信息'''


# 默认
def empInfoFillIn(request):
    idCard = request.POST.get('idCard')
    name = request.POST.get('name')
    code = request.POST.get('code')
    phone = request.POST.get('phone')
    politicsStatus = request.POST.get('politicsStatus')
    nationality = request.POST.get('nationality')
    nationId = request.POST.get('nationId')
    domicileAddress = request.POST.get('domicileAddress')
    nativePlaceId = request.POST.get('nativePlaceId')
    latestAddress = request.POST.get('latestAddress')
    marriage = request.POST.get('marriage')
    urgentPerson = request.POST.get('urgentPerson')
    urgentPersonPhone = request.POST.get('urgentPersonPhone')
    urgentRelation = request.POST.get('urgentRelation')
    firstDegreeId = request.POST.get('firstDegreeId')
    graduateTime = request.POST.get('graduateTime')
    graduateInsituation = request.POST.get('graduateInsituation')
    major = request.POST.get('major')
    bankIdCard = request.POST.get('bankIdCard')
    openBank = request.POST.get('openBank')
    educateMethod = request.POST.get('educateMethod')  # 教育方式
    bankCardPicList = request.FILES.getlist('bankCardPicList')  # 银行卡照片
    idCardPicList = request.FILES.getlist('idCardPicList')  # 身份证照片
    resumeFile = request.FILES.getlist('resumeFile')  # 简历文件
    diplomaPicList = request.FILES.getlist('diplomaPicList')  # 毕业证书照片
    dissmissPf = request.FILES.getlist('dissmissPf')  # 离职证明
    oneInchPic = request.FILES.getlist('oneInchPic')  # 一寸照片
    affixMaterialList = request.FILES.getlist('affixMaterialList')  # 非用印材料
    # idCardPicList = request.POST.get('idCardPicList')
    # bankCardPicList = request.POST.get('bankCardPicList')
    # resumeFile = request.POST.get('resumeFile')
    new_emp_info = EmpInfoFillIn()
    # emp_info = new_emp_info.empInfoFillIn(emp_info['idCard'],emp_info['name'],emp_info['code'],emp_info['politicsStatus'],emp_info['nationality'],emp_info['nationId'],emp_info['domicileAddress'],emp_info['nativePlaceId'],emp_info['latestAddress'],emp_info['marriage'],emp_info['urgentPerson'],emp_info['urgentPersonPhone'],emp_info['urgentRelation'],emp_info['firstDegreeId'],emp_info['graduateTime'],emp_info['graduateInsituation'],emp_info['major'],emp_info['educateMethod'],emp_info['bankIdCard'],emp_info['openBank'],emp_info['idCardPicList'],emp_info['bankCardPicList'],emp_info['resumeFile'])
    emp_info = new_emp_info.empInfoFillIn(idCard, name, code, politicsStatus, nationality, nationId, domicileAddress, nativePlaceId, latestAddress, marriage, urgentPerson, urgentPersonPhone,
                                          urgentRelation, firstDegreeId, graduateTime, graduateInsituation, major, educateMethod, bankIdCard, openBank, idCardPicList, bankCardPicList, resumeFile,
                                          phone, diplomaPicList, dissmissPf, oneInchPic, affixMaterialList)
    print(emp_info)
    return HttpResponse(json.dumps(emp_info))


'''人员信息批量导入'''


def empInfoImport(request):
    upload_file = request.FILES.get('file')
    print(upload_file)
    token = request.headers['token']
    print(token)
    # new_import_info = EmpInfoFillIn()
    # import_info = new_import_info.empInfoImport(emp_info['idList'])
    # return HttpResponse(import_info)
    return HttpResponse(1)


'''白名单人员查询'''


def whiteListPerson(request):
    token = request.headers['token']
    emp_info = json.loads(request.body)
    emp_info_obj = EmpInfoFillIn()
    query_person = emp_info_obj.whiteListPerson(token, emp_info['empInfo'], emp_info['period'], emp_info['limit'], emp_info['page'])
    return HttpResponse(json.dumps(query_person))


'''根据身份证返回该人员信息'''


def empFillInQuery(request):
    info = json.loads(request.body)
    emp_obj = EmpInfoFillIn()
    query_info = emp_obj.empFillInQuery(info['idCard'])
    return HttpResponse(json.dumps(query_info))


'''白名单人员修改'''


def whiteListPersonModify(request):
    modify_list = json.loads(request.body)
    modify_obj = EmpInfoFillIn()
    modify_info = modify_obj.whiteListPersonModify(modify_list['id'],modify_list['empName'],modify_list['empCode'],modify_list['empEntryDate'],modify_list['deptId'],modify_list['postId'],modify_list['jobDutyId'],modify_list['jobClassId'],modify_list['jobGradeId'],modify_list['jobRankId'],modify_list['empGrade'],modify_list['empPredictEntryDate'],modify_list['ethnicId'],modify_list['nativePlaceId'],modify_list['payTypeId'],modify_list['firstDegreeId'],modify_list['empIdCard'],modify_list['empGender'],modify_list['empMarriage'],modify_list['empBirthDay'],modify_list['empPhone'],modify_list['empDlIDL'],modify_list['empLaborSource'],modify_list['empPoliticalAffiliation'],modify_list['empNationality'],modify_list['empDomicileAddress'],modify_list['empLatestAddress'],modify_list['empUrgentPerson'],modify_list['empUrgentPhone'],modify_list['empUrgentRelation'],modify_list['empGraduateTime'],modify_list['empGraduateInsituation'],modify_list['empMajor'],modify_list['empEducateMethod'],modify_list['empBankCardNum'],modify_list['empBank'])
    return HttpResponse(json.dumps(modify_info))


'''下拉列表'''

'''民族下拉列表'''


def nationDrop(request):
    nation_obj = DropList()
    nation_result = nation_obj.nationDrop()
    return HttpResponse(json.dumps(nation_result))


'''获取籍贯列表'''


def nativeDrop(request):
    native_obj = DropList()
    native_result = native_obj.nationDrop()
    return HttpResponse(json.dumps(native_result))


def approve(request):
    """
    审批
    :param request:
    :return:
    """
    token = request.headers['token']
    request_data = json.loads(request.body.decode('utf-8'))
    scan = EmpInfoFillIn()
    return_data = scan.approve(token, request_data['id_list'], request_data['status'])
    return HttpResponse(json.dumps(return_data))


def get_select_options(request):
    token = request.headers['token']
    data = select_options(token)
    return HttpResponse(json.dumps(data))
